package cn.dnf.system.controller;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.PageList;
import cn.dnf.system.domain.Menu;
import cn.dnf.system.domain.Role;
import cn.dnf.system.query.RoleQuery;
import cn.dnf.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/role")
@Api(value = "菜单的API",description="菜单相关的CRUD功能")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 添加或修改
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.save(role);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有菜单信息" )
    public List<Role> findAll(){

        return roleService.loadAll();
    }
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Role> queryPage(@RequestBody RoleQuery query){
        try {
            // 哈哈哈
            return roleService.pageQuery(query);
            // ....
            // .....
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Role>();
        }
    }


    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            roleService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            roleService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }


}
